package cn.snmp.mutual.uk.event;

import java.util.*;


import cn.snmp.mutual.uk.stack.Pdu;

public class TrapReceivedSupport {
    public static final String version_id = "@(#)$Id: TrapReceivedSupport.java,v 1.6 2006/01/17 17:43:53 birgit Exp $ Copyright Westhawk Ltd";
    private Object source;
    private transient Vector trapListeners;

    public TrapReceivedSupport(final Object src) {
        this.source = src;
    }

    public synchronized void empty() {
        if (this.trapListeners != null) {
            this.trapListeners.removeAllElements();
        }
    }

    public synchronized int getListenerCount() {
        int c = 0;
        if (this.trapListeners != null) {
            c = this.trapListeners.size();
        }
        return c;
    }

    public synchronized void addTrapListener(final TrapListener listener) {
        if (this.trapListeners == null) {
            this.trapListeners = new Vector(5);
        }
        if (!this.trapListeners.contains(listener)) {
            this.trapListeners.addElement(listener);
        }
    }

    public synchronized void removeTrapListener(final TrapListener listener) {
        if (this.trapListeners != null) {
            this.trapListeners.removeElement(listener);
        }
    }

    public void fireTrapReceived(final Pdu pdu, final int hostPort, final RawPduEvent pduEvt) {
        Vector copyOfListeners = null;
        if (this.trapListeners != null) {
            synchronized (this.trapListeners) {
                copyOfListeners = (Vector) this.trapListeners.clone();
            }
        }
        if (copyOfListeners != null) {
            final int sz = copyOfListeners.size();
            for (int i = sz - 1; i >= 0; --i) {
                final TrapListener listener = (TrapListener) copyOfListeners.elementAt(i);
                final TrapEvent evt = new TrapEvent(this.source, pdu, hostPort);
                listener.trapReceived(evt, pduEvt);
            }
        }
    }
}
